﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Company.Web.Commen
{
    public static class StringExtension
    {
        /// <summary>
        /// 生成弹出提示框的Javascript
        /// </summary>
        /// <param name="str">要提示的内容</param>
        /// <returns></returns>
        public static string AlertOffPage(this string str)
        {
            return "<script>$(document).ready(function () {alert('" + str + "');});</script>";
        }
        /// <summary>
        /// 生成弹出提示框的Javascript
        /// </summary>
        /// <param name="str">要提示的内容</param>
        /// <param name="active">要进行其它操作的Javascript内容</param>
        /// <param name="isFirst">active是否要提前执行</param>
        /// <returns></returns>
        public static string AlertOffPage(this string str, string active, bool isFirst)
        {
            if (active.ToLower().IndexOf("<script>".ToLower()) >= 0)
            {
                int index1 = active.IndexOf('>');
                int index2 = active.LastIndexOf('<');
                active = active.Substring(index1 + 1, index2 - index1 - 1);
            }
            var value = "<script>$(document).ready(function () {alert('" + str + "');});" + active + ";</script>";
            if (isFirst)
            {
                value = "<script>" + active + ";$(document).ready(function () {alert('" + str + "');});</script>";
            }
            return value;
        }
        /// <summary>
        /// 重定向到此地址
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="msg">重定向后要弹出的提示框的内容(alert)</param>
        /// <returns></returns>
        public static string Redirect(this string url, string msg)
        {
            return "<script>" + (string.IsNullOrEmpty(msg) ? "" : ("alert('" + msg + "')")) + ";" + "window.location.href=" + (string.IsNullOrEmpty(url) ? "window.location.href" : ("'" + url + "'")) + ";</script>";
        }
        /// <summary>
        /// 重定向到此地址
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="msg">重定向后要弹出的提示框的内容(confirm)</param>
        /// <param name="url2">当前用户选择否是要重定向的地址</param>
        /// <returns></returns>
        public static string Redirect(this string url, string msg, string url2)
        {
            var locationUrl = "";
            var locationUrl2 = "";
            if (string.IsNullOrEmpty(url))
                locationUrl = "window.location.href";
            else
                locationUrl = "'" + url + "'";
            if (!string.IsNullOrEmpty(url2))
                locationUrl2 = string.Format("window.location.href='{0}';", url2);

            return string.Format("<script>{0}</script>",
                    string.IsNullOrEmpty(msg)
                        ? string.Format("window.location.href={0};", locationUrl)
                        : "if(confirm('" + msg + "')){window.location.href=" + locationUrl + ";}else{" + locationUrl2 + "}");
        }
        /// <summary>
        /// 重定向到此地址
        /// </summary>
        /// <param name="url">地址</param>
        /// <returns></returns>
        public static string Redirect(this string url)
        {
            return "<script>window.location.href=" + (url == "" ? "window.location.href" : ("'" + url + "'")) + ";</script>";
        }
    }
}